import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../helper/Helper.dart';
import '../../helper/cacheHelper.dart';
import '../../helper/channelHelper.dart';
import '../../helper/downloadHelper.dart';

import '../../helper/fileHelper.dart';
import '../../helper/uidata.dart';

import '../../pages/skddl/sdddl_authorty.dart';
import '../../pages/skddl/skddl_info_huizhi.dart';
import '../../pages/skddl/skddl_list.dart';
import '../../widgets/dialogWidget.dart';
import '../../widgets/signatureCard.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../helper/global_config.dart';

import '../../model/skddl.dart';
import '../../widgets/IconWidget.dart';

class SKDDLInfoPage extends StatefulWidget {
  @override
  SKDDLInfoPage(this.s1, this.callback);
  final SKDDL s1;
  final RefrushCallback callback;

  @override
  createState() => new SKDDLInfoPageState();
}

class SKDDLInfoPageState extends State<SKDDLInfoPage> {
  late SKDDL s1;
  final TextEditingController c2 = new TextEditingController();

  @override
  void initState() {
    super.initState();
    //s1 = widget.s1;
    //UIData.CurrentUser!.Authority="水调值班员,电厂值班员";
    loadData();
  }

  loadData() async {
    var s2 = await SKDDL.Get("ID=@ID", {"ID": widget.s1.ID});
    setState(() {
      s1 = s2 as SKDDL;
    });
  }

  _callPhone(String p) async {
    await launch("tel:$p");
  }

  upload(File file1) async {
    await Helper.GetstoragePermission();
    var imgId = await FileHelper.UpLoad(file1);

    if (s1.State == 0 && s1.BMUserID == UIData.CurrentUser!.Guid) {
      s1.BMIfOK = true;
      s1.BMTM = Helper.DateTimeNowString();
      s1.BMQM = imgId;
      s1.BMHQ = "同意";
      s1.State = 1;
      await SKDDL.SendById(s1.Title, "需要会签", s1.DCUserID);
      await SKDDL.SendById(s1.Title, "需要会签", s1.GCBUserID);
    }

    if (s1.State == 1 && s1.DCUserID == UIData.CurrentUser!.Guid) {
      s1.DCIfOK = true;
      s1.DCTM = Helper.DateTimeNowString();
      s1.DCQM = imgId;
      s1.DCHQ = "同意";
      if (s1.GCBIfOK) {
        s1.State = 2;
        await SKDDL.SendById(s1.Title, "需要签发", s1.LDUserID);
      }
    }

    if (s1.State == 1 && s1.GCBUserID == UIData.CurrentUser!.Guid) {
      s1.GCBIfOK = true;
      s1.GCBTM = Helper.DateTimeNowString();
      s1.GCBQM = imgId;
      s1.GCBHQ = "同意";
      if (s1.DCIfOK) {
        s1.State = 2;
        await SKDDL.SendById(s1.Title, "需要签发", s1.LDUserID);
      }
    }

    if (s1.State == 2 && s1.LDUserID == UIData.CurrentUser!.Guid) {
      s1.LDIfOK = true;
      s1.LDTM = Helper.DateTimeNowString();
      s1.LDQM = imgId;
      s1.LDHQ = "同意";
      s1.State = 3;
      await SKDDL.SendById(s1.Title, "已签发完成！", s1.BMUserID);
    }

    SKDDL.Update(s1);
    var s2 = await SKDDL.Get("ID=@ID", {"ID": s1.ID});
    if (s2 != null) {
      setState(() {
        s1 = s2;
      });
    }
    widget.callback();
    ChannelHelper.showToast("签字成功！");
  }

  Widget getCLWidget(bool ifOk, String content, String imgId) {
    if (!ifOk)
      return ListTile(
          title: Text(
        "未处理",
        style: TextStyle(color: Colors.black54, height: 2.0),
      ));
    return ListTile(
      title: Text(
        content,
        style: TextStyle(color: Colors.black54, height: 2.0),
      ),
      trailing: Image.network(Helper.GetImgUrl(imgId)),
    );
  }

  Widget getDDUser(String title, String userId) {
    var u1 = CacheHelper.GetUser(userId);
    return ListTile(
      title: Text(title),
      trailing: Text(u1.Name),
      onTap: () {
        if (u1.Phone == null) return;

        showDialog(
            context: context,
            builder: (BuildContext context) {
              return DialogWidget.getDialog(
                  context, "拨打电话", "点击确定将拨打电话给" + u1.Name, () {
                _callPhone(u1.Phone);

                ;
              });
            });
      },
    );
  }

  List<Widget> getWidgetList() {
    List<Widget> list1 = <Widget>[];
    list1.add(getDDUser("调度令审核", s1.BMUserID));
    list1.add(getCLWidget(s1.BMIfOK, Helper.DateTimeFormat(s1.BMTM), s1.BMQM));
    if (!s1.BMIfOK) return list1;
    list1.add(Divider(
      height: 0.0,
    ));
    list1.add(getDDUser("电厂会签", s1.DCUserID));
    list1.add(getCLWidget(s1.DCIfOK, Helper.DateTimeFormat(s1.DCTM), s1.DCQM));
    list1.add(Divider(
      height: 0.0,
    ));
    list1.add(getDDUser("工程部会签", s1.GCBUserID));
    list1.add(
        getCLWidget(s1.GCBIfOK, Helper.DateTimeFormat(s1.GCBTM), s1.GCBQM));
    list1.add(Divider(
      height: 0.0,
    ));
    if (!s1.GCBIfOK || !s1.DCIfOK) return list1;

    list1.add(getDDUser("调度令签发", s1.LDUserID));
    list1.add(getCLWidget(s1.LDIfOK, Helper.DateTimeFormat(s1.LDTM), s1.LDQM));
    return list1;
  }

  void showPanel(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SignatureCardPage(
            upload, SkddlAuthority.QianZiTitle(s1, UIData.CurrentUser!));
      },
    );
  }

  Widget getFloatingButton(BuildContext context) {
    if (s1 == null) return new Container();
    if (SkddlAuthority.IfQianZi(s1, UIData.CurrentUser!)) {
      return FloatingActionButton(
          child: Icon(FontAwesomeIcons.penAlt),
          onPressed: () {
            showPanel(context);
          });
    }

    return new Container();
  }

  @override
  Widget build(BuildContext context) {
    // if (s1 == null) return Center(child:SpinKitCircle(color: Colors.blue,));

    // TODO: implement build
    return Scaffold(
        // appBar: AppBar(title: Text('调度令详情')),
        //bottomSheet:bottomPanel2() ,
        floatingActionButton: getFloatingButton(context),
        body: s1 == null
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : SingleChildScrollView(
                child: new Container(
                    margin: EdgeInsets.only(top: 8.0),
                    child: new Column(children: <Widget>[
                      Card(
                        child: ListTile(
                          leading: IconWidget.SkddlImage(),
                          title: Text(s1.Title),
                        ),
                        color: GlobalConfig.cardBackgroundColor,
                        margin: const EdgeInsets.all(6.0),
                      ),
                      Card(
                        child: Column(
                          children: <Widget>[
                            ListTile(
                              title: Text("调度令简述"),
                            ),
                            Divider(
                              height: 0.0,
                            ),
                            ListTile(
                              title: Text(
                                s1.Content,
                                style: TextStyle(
                                    color: Colors.black54, height: 2.0),
                              ),
                              // subtitle: Text(Helper.DateTimeFormat(s1.CreateTime)),
                            ),
                            Divider(
                              height: 0.0,
                            ),
                            ListTile(
                              title: Text(
                                Helper.DateTimeFormat(s1.CreateTime),
                                style: TextStyle(
                                    color: Colors.black54, height: 1.0),
                              ),
                              trailing: Text(
                                s1.ZBYName,
                                style: TextStyle(
                                    color: Colors.black54, height: 1.0),
                              ),
                            ),
                          ],
                        ),
                        color: GlobalConfig.cardBackgroundColor,
                        margin: const EdgeInsets.all(6.0),
                      ),
                      Card(
                        child: Column(
                          children: getWidgetList(),
                        ),
                        color: GlobalConfig.cardBackgroundColor,
                        margin: const EdgeInsets.all(6.0),
                      ),
                    ])),
              ));
  }
}
